package hms.master;

import hms.UserAccountsModule.UserAccount;
import hms.UserAccountsModule.UserAccountControl;
import hms.generic.GUIViewBuilder;
import hms.properties.Properties;

import java.util.Arrays;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class LoginController {
	
	private JTextField usernameField;
	private JPasswordField passwordField;
	private JLabel usernameLabel;
	private JLabel passwordLabel;
	
	// Object containing list of all users
	private UserAccountControl users;
	// Object that checks if user credentials are correct and then stores the
	// users information locally before storing globally
	private UserAccount userLoggingIn;
	
	// Main GUIViewBuilder for program
	GUIViewBuilder builder;
	
	public LoginController(GUIViewBuilder mainBuilder){
		
		// Assign GUI to local variable
		builder = mainBuilder;
		
		// Create the login JOptionPane if logging-in
		if(!Properties.isLoggedIn)
			createLoginDialog();
		else if(Properties.isLoggedIn)
			createLogoutDialog();
		
	}
	
	private void createLoginDialog(){
		
		usernameField = new JTextField();
		passwordField = new JPasswordField();
		
		usernameLabel = new JLabel("Username:");
		passwordLabel = new JLabel("Password:");

		Object[] componentsArray = { usernameLabel, 
                usernameField,
                passwordLabel,
                passwordField};
		
		Object[] options = {"Login", "Cancel"};
		
		int response = JOptionPane.showOptionDialog(null, 
				componentsArray, 
				"Login",
				JOptionPane.OK_CANCEL_OPTION,
                JOptionPane.PLAIN_MESSAGE,
                null,
                options,
                componentsArray[1]);
		
		if(response == JOptionPane.YES_OPTION){	
			validateLogin();
		}
	}
	
	
	private void createLogoutDialog(){
		
		Object[] options = {"Yes", "Cancel"};
		
		int response = JOptionPane.showOptionDialog(null, 
				"Are you sure you want to logout?",
				"Logout",
				JOptionPane.OK_CANCEL_OPTION,
				JOptionPane.PLAIN_MESSAGE,
				null,
				options,
				options[0]);
		
		if(response == JOptionPane.YES_OPTION){
			
			// Change the global property so that the user is not logged in
			Properties.isLoggedIn = false;
			
			// TODO:  Clear static data for currently logged in user (matt)
			// TODO:  Return to splash screen, clear all stored data and make any changes here
			
			// Change the text of the Login menu item.
			builder.updateLoginButton();
			builder.destroy();
			JPanel mainPanel = new JPanel();
			
			String strHMSLogo = "<html><div style=\"background-color:#CFAC1F; vertical-align: middle;\"><h1 style=\"text-align: center\">"
					+ "<span style=\"font-family: Arial, Helvetica, sans-serif;\">H<span style=\"font-size: large; font-style: italic; "
					+ "font-family: Arial, Helvetica, sans-serif;\">OTEL</span><br /><span style=\"font-family: Arial, Helvetica, "
					+ "sans-serif;\">M<span style=\"font-size: large; font-style: italic; font-family: Arial, Helvetica, sans-serif;\">"
					+ "ANAGEMENT</span><br /><span style=\"font-family: Arial, Helvetica, sans-serif;\">S<span style=\"font-size: large; "
					+ "font-style: italic; font-family: Arial, Helvetica, sans-serif;\">YSTEM</span></h1><div style=\"background-color: "
					+"#E0C962; padding-right: 50px;  padding-left: 50px;\"><h2>Team MAYBE</h2><p>Matt Johnson<br />Akilah McIntyre<br />"
					+"Yifeng Zou<br />Ben Adcock<br />Edgar Onukwugha<br /></p><p>&nbsp;</p><p>&nbsp;</p></div></div></html>";
			mainPanel.add(new JLabel(strHMSLogo));
			builder.update(mainPanel);
			builder.display();
			
		}
	}
	
	private void validateLogin(){

		// Store the values from the input fields locally
		String i_username = usernameField.getText();
		String i_password = new String(passwordField.getPassword());

		// Initialize list of users
		users = new UserAccountControl();
		
		userLoggingIn = users.getByUsername(i_username);
		
		// Check log-in information and store user in this object. If login
		// information is incorrect, stores null value
		//userLoggingIn = users.authenticateUser(i_username, i_password);
		
		boolean TEST = users.authenticateUser(i_username, i_password) != null;

		// If user does not exist in the database
		// TODO: [MATT] Find out what is wrong with validation

		if(users.authenticateUser(i_username, i_password) != null){
			loginIsValidated();
		}
		// If user exists in the database
		else{
			loginIsNotValidated();
		}

	}
	
	// TODO: [matt] Add comments
	private void loginIsValidated(){
		
		// Set global property of being logged in 
		Properties.isLoggedIn = true;
		
		// Set global value of user account that is logged in
		Properties.currentUser = userLoggingIn;
		
		// Change login text in menu
		builder.updateLoginButton();

	}
	
	// TODO: [matt] Add comments
	private void loginIsNotValidated(){
		
		// Display a dialog message for incorrect login information
		JOptionPane.showMessageDialog(null, "Incorrect username and password combination, plase try again!",
				"Invalid Login", 
				JOptionPane.INFORMATION_MESSAGE);
	}

}
